<template>
  <div>
    <van-nav-bar
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
      class="page-nav-bar"
    />
    <input
      type="file"
      hidden
      ref="input"
      accept=".png,.jpg"
      @change="onSlected"
    />
    <van-cell is-link title="头像" @click="$refs.input.click()">
      <van-image round width="0.8rem" height="0.8rem" :src="userInfo.photo" />
    </van-cell>
    <van-cell is-link title="昵称" @click="isShowNcname = true">
      <span>{{ userInfo.name }}</span>
    </van-cell>
    <van-cell is-link title="性别" @click="isShowGender = true">
      <span>{{ userInfo.gender === 0 ? '男' : '女' }}</span>
    </van-cell>
    <van-cell is-link title="生日" @click="isShowBirthday = true">
      <span>{{ userInfo.birthday }}</span>
    </van-cell>
    <!-- 更新头像 -->
    <van-popup
      v-model="isShowAvatar"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-avatar
        :photo="photo"
        @close="isShowAvatar = false"
        v-if="isShowAvatar"
        @update-photo="userInfo.photo = $event"
      ></update-avatar>
    </van-popup>
    <!-- 更新昵称 -->
    <van-popup
      v-model="isShowNcname"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-ncname
        v-if="userInfo.name && isShowNcname"
        :name.sync="userInfo.name"
        @close="isShowNcname = false"
      ></update-ncname>
    </van-popup>
    <!-- 更新性别 -->
    <van-popup
      v-model="isShowGender"
      position="bottom"
      :style="{ height: '50%' }"
    >
      <update-gender
        v-if="userInfo.name && isShowGender"
        :gender.sync="userInfo.gender"
        @close="isShowGender = false"
      ></update-gender>
    </van-popup>
    <!-- 更新生日 -->
    <van-popup
      v-model="isShowBirthday"
      position="bottom"
      :style="{ height: '50%' }"
    >
      <update-birthday
        v-if="userInfo.birthday && isShowBirthday"
        :birthday.sync="userInfo.birthday"
        @close="isShowBirthday = false"
      ></update-birthday>
      <!-- 语法糖 v-model 相当于绑定了一个事件和传了一个值 -->
      <!-- <update-birthday
        v-if="userInfo.birthday && isShowBirthday"
        v-model="userInfo.birthday"
        @close="isShowBirthday = false"
      ></update-birthday> -->
    </van-popup>
  </div>
</template>

<script>
import { getUserProfileAPI } from '@/api'
import { resolveToBase64 } from '@/utils'
import UpdateBirthday from './components/UpdateBirthday.vue'
import UpdateNcname from './components/UpdateNcname.vue'
import UpdateGender from './components/UpdateGender.vue'
import UpdateAvatar from './components/UpdateAvatar.vue'
export default {
  name: '',
  components: { UpdateBirthday, UpdateNcname, UpdateGender, UpdateAvatar },
  props: {},
  data() {
    return {
      userInfo: {},
      isShowAvatar: false,
      isShowNcname: false,
      isShowGender: false,
      isShowBirthday: false,
      photo: ''
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getUserProfile()
  },
  methods: {
    async getUserProfile() {
      const { data } = await getUserProfileAPI()
      this.userInfo = data.data
    },
    async onSlected() {
      const file = this.$refs.input.files[0]
      const base64 = await resolveToBase64(file)
      this.photo = base64
      this.isShowAvatar = true
      this.$refs.input.value = ''
    }
  }
}
</script>

<style scoped lang="less">
:deep(.page-nav-bar) {
  background-color: #3296fa;
  .van-icon {
    color: #fff;
  }
  .van-nav-bar__title {
    color: #fff;
  }
}
</style>
